package handler

import (
    "fcs-api/src/app/business"
    "fcs-api/src/app/model"
    "net/http"

    i18n "git.inspur.com/x/common/i18n"
    "github.com/emicklei/go-restful"
)

func (api *APIHandler) handleCreateAlias(request *restful.Request, response *restful.Response) {
    alias, err := validateCreateAlias(request)
    if err != nil {
        handleInternalError(response, err)
        return
    }
    fcClient, err := GetFcClient(api)
    if err != nil {
        _ = logger.Error("handleCreateAlias: get cmanagerconfig error %v", err)
        handleInternalError(response, model.InternalServerError(model.FunctionAliasError, i18n.GetMessage(request.Request, "fcs.alias.create")))
        return
    }
    alias, err = business.CreateAlias(fcClient, alias)
    if err != nil {
        handleInternalError(response, model.InternalServerError(model.FunctionAliasError, i18n.GetMessage(request.Request, "fcs.alias.create")))
        return
    }
    _ = response.WriteHeaderAndEntity(http.StatusOK, alias)
}

func (api *APIHandler) handleUpdateAlias(request *restful.Request, response *restful.Response) {
    alias, err := validateUpdateAlias(request)
    if err != nil {
        if model.IsNoNeedUpdate(err) {
            _ = response.WriteHeaderAndEntity(http.StatusOK, alias)
            return
        }
        handleInternalError(response, err)
        return
    }
    fcClient, err := GetFcClient(api)
    if err != nil {
        _ = logger.Error("handleCreateAlias: get cmanagerconfig error %v", err)
        handleInternalError(response, model.InternalServerError(model.FunctionAliasError, i18n.GetMessage(request.Request, "fcs.alias.update")))
        return
    }
    alias, err = business.UpdateAlias(fcClient, alias)
    if err != nil {
        handleInternalError(response, model.InternalServerError(model.FunctionAliasError, i18n.GetMessage(request.Request, "fcs.alias.update")))
        return
    }
    _ = response.WriteHeaderAndEntity(http.StatusOK, alias)
}

func (api *APIHandler) handleGetAliasList(request *restful.Request, response *restful.Response) {
    logger.Debug("handleGetAliasList start.")
    from := request.QueryParameter("from")
    functionInfo, err := validateFuncInfo(request)
    if err != nil {
        if from == "apig" {
            _ = response.WriteHeaderAndEntity(http.StatusOK, map[string]interface{}{"data": []*model.FuncAlias{}})
            return
        }
        handleInternalError(response, err)
        return
    }
    aliasList, err := business.GetAliasList(functionInfo.Id)
    if err != nil {
        handleInternalError(response, err)
        return
    }
    logger.Debug("handleGetAliasList end.")
    _ = response.WriteHeaderAndEntity(http.StatusOK, map[string]interface{}{"data": aliasList})
}

func (api *APIHandler) handleDeleteAlias(request *restful.Request, response *restful.Response) {
    alias, err := validateDeleteAlias(request)
    if err != nil {
        handleInternalError(response, err)
        return
    }
    fcClient, err := GetFcClient(api)
    if err != nil {
        _ = logger.Error("handleDeleteAlias: get cmanagerconfig error %v", err)
        handleInternalError(response, model.InternalServerError(model.FunctionAliasError, i18n.GetMessage(request.Request, "fcs.alias.update")))
        return
    }
    err = business.DeleteAlias(fcClient, alias)
    if err != nil {
        handleInternalError(response, err)
        return
    }
    response.WriteHeader(http.StatusOK)
}

func (api *APIHandler) handleCheckAliasExist(request *restful.Request, response *restful.Response) {
    aliasName := request.PathParameter("aliasName")
    functionId := request.PathParameter("functionId")

    _, err := validateFuncInfo(request)
    if err != nil {
        handleInternalError(response, err)
        return
    }

    exist, err := model.CheckAliasExist(functionId, aliasName)
    if err != nil {
        _ = logger.Error("handleCheckAliasExist CheckAliasExist error %v", err)
    }
    _ = response.WriteHeaderAndEntity(http.StatusOK, map[string]interface{}{"exist": exist})
}
